package com.gitee.webdav.kernel;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.text.StrPool;
import com.google.auto.service.AutoService;
import org.smartboot.http.common.enums.HeaderNameEnum;
import org.smartboot.http.server.HttpRequest;
import org.smartboot.http.server.HttpResponse;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @author hongda.li@hand-china.com
 */
@AutoService(ResourceReadonlyHandler.class)
public class ResourceOptions extends ResourceReadonlyHandler {
    /**
     * 所有允许的方法名称，用逗号分隔
     */
    private final String allowedMethodNames = Arrays.stream(MethodEnum.values())
            .map(Enum::name)
            .collect(Collectors.joining(StrPool.COMMA + CharSequenceUtil.SPACE));

    @Override
    protected MethodEnum method() {
        return MethodEnum.OPTIONS;
    }

    @Override
    public void handle(HttpRequest request, HttpResponse response) {
        response.addHeader(HeaderNameEnum.ALLOW.getName(), this.allowedMethodNames);
        response.addHeader(Constants.HttpHeader.DAV, Constants.HttpHeader.DAV_LEVEL_ONE);
    }
}
